package com.company.manage.system.web.controller;

import com.company.manage.common.utils.Global;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Created by HuangYong on 2016/1/23.
 * desc:
 */
@Controller
@RequestMapping(value = "/admin")
public class LoginController{
    private static final String TAG = "LoginController";


    @RequestMapping(value = "login", method = RequestMethod.GET)
    public String login() {
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated() || subject.isRemembered()) {
            return "redirect:" + Global.getAdminPath();
        }
        return "system/login";
    }

    /**
     * 登录失败提示
     *
     * @param username
     * @param model
     * @return
     */
    @RequestMapping(value = "login", method = RequestMethod.POST)
    public String fail(@RequestParam(FormAuthenticationFilter.DEFAULT_USERNAME_PARAM) String username, Model model) {
        model.addAttribute(FormAuthenticationFilter.DEFAULT_USERNAME_PARAM, username);
        return "system/login";
    }

    /**
     * 登出系统
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "logout")
    public String logOut(Model model) {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "system/login";
    }
}